package com.sphd.education.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.sphd.education.R;
import com.sphd.education.activitys.SchoolMsgsActivity;
import com.sphd.education.info.MsgInfo;
import com.sphd.education.modle.EducationUrl;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hu on 2016/2/1.
 */
public class MsgsAaapter extends BaseAdapter {
    Context context;
    List<MsgInfo> schoolMsgs;
    private boolean isShowing;
    private List<Integer> mPos =new ArrayList<>();
    public List<Integer> getmPos() {
        return mPos;
    }
    public MsgsAaapter(Context context, List<MsgInfo> schoolMsgs) {
        this.context = context;
        this.schoolMsgs = schoolMsgs;
    }

    public boolean isShowing() {
        return isShowing;
    }

    public void setIsShowing(boolean isShowing) {
        this.isShowing = isShowing;
    }

    @Override
    public int getCount() {
        return schoolMsgs.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.msg_item, null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        viewHolder.tchName.setText(schoolMsgs
                .get(position).getOrgName()+"-"+schoolMsgs.get(position).getPost()+"-"+schoolMsgs.get(position).getCreatName());
        viewHolder.tchTitle.setText(schoolMsgs.get(position).getTitle());
        ImageLoader.getInstance().displayImage(EducationUrl.HttpTitle + "/" + schoolMsgs.get(position).getPathUrl(), viewHolder.tchImage, EducationUrl
                .setImg());
        SimpleDateFormat ss = new SimpleDateFormat("MM-dd");//12小时制
        viewHolder.tchDate.setText(ss.format(new Date(schoolMsgs.get(position).getCreatTime())));
        viewHolder.msgCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent  intent =new Intent();
                intent.setClass(context, SchoolMsgsActivity.class);
                intent.putExtra("schmsg", (Serializable) schoolMsgs.get(position));
                context.startActivity(intent);
            }
        });

        if (isShowing){
            viewHolder.checkBoxSchool.setVisibility(View.VISIBLE);
            mPos.clear();
        }else {
            viewHolder.checkBoxSchool.setVisibility(View.GONE);
            mPos.clear();
        }
        viewHolder.checkBoxSchool.setChecked(false);
        if (mPos.contains((Integer)position)){
            viewHolder.checkBoxSchool.setChecked(true);
        }
        final ViewHolder finalViewHolder = viewHolder;
        viewHolder.checkBoxSchool.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (finalViewHolder.checkBoxSchool.isChecked()){
                    mPos.add((Integer)position);
                }else {
                    mPos.remove((Integer)position);
                }

            }
        });

        return convertView;
    }


    static class ViewHolder {
        @Bind(R.id.tch_image)
        ImageView tchImage;
        @Bind(R.id.tch_name)
        TextView tchName;
        @Bind(R.id.tch_title)
        TextView tchTitle;
        @Bind(R.id.tch_date)
        TextView tchDate;
        @Bind(R.id.msg_card)
        CardView msgCard;
        @Bind(R.id.checkbox_school)
        CheckBox checkBoxSchool;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
